<?php
class ControllerDeployQuotas extends Controller {
    public function index() {

        $this->language->load('deploy/quotas');

        $json=array();

        foreach($this->session->data['app']['eip_deploy'] as $result){
            $eip_array[$result['params']['region']][]=$result;
        }
        foreach($this->session->data['app']['vpc_deploy'] as $result){
            $vpc_array[$result['params']['region']][]=$result;
        }
        foreach($this->session->data['app']['deploy_config'] as $result){
            $instance_array[$result['params']['region']][]=$result;
        }

        foreach($this->session->data['app']['eip_deploy'] as $result){
            $region=$result['params']['region'];

            $eip_count=count($eip_array[$region]);
            $vpc_count=count($vpc_array[$region]);
            $instance_count=count($instance_array[$region]);


            $quotas=$this->backend->getquotas($this->customer->getEmail(),$region);

            if($region!='qingcloud_ap1'&&!empty($quotas)){

                if($eip_count>$quotas['public_ip']){
                    $json['error'] .= $this->language->get('error_public_ip')."<br/>";
                }
                if($vpc_count>$quotas['vpc']){
                    $json['error'] .= $this->language->get('error_vpc')."<br/>";
                }
                if($instance_count>$quotas['instance']){
                    $json['error'] .= $this->language->get('error_instance')."<br/>";
                }
            }
            
            if($region == 'qingcloud_ap1'){
            	$quotas_limits_ap=$this->backend->getquotalimits($this->customer->getEmail(),'qingcloud_ap1');
            	$quotas_ap=$this->backend->getUsedquotas($this->customer->getEmail(),'qingcloud_ap1');
        	    if(!isset($quotas_limits_ap['public_ip'])){
		
		            $quotas_limits_ap['public_ip'] = 1;
		        }
		        if(!isset($quotas_limits_ap['vpc'])){
		            $quotas_limits_ap['vpc'] = 1;
		        }
		
		        if(!isset($quotas_limits_ap['instance'])){
		            $quotas_limits_ap['instance'] = 3;
		        }
		
		
		        if(!isset($quotas_ap['public_ip'])){
		            $quotas_ap['public_ip'] = 0;
		
		        }
		        if(!isset($quotas_ap['vpc'])){
		            $quotas_ap['vpc'] = 0;
		        }

		        if(!isset($quotas_ap['instance'])){
		            $quotas_ap['instance'] = 0;
		        }
		        
		        if($eip_count > ($quotas_limits_ap['public_ip'] - $quotas_ap['public_ip'])){
                    $json['error'] .= $this->language->get('error_public_ip')."<br/>";
                }
                if($vpc_count > ($quotas_limits_ap['vpc'] - $quotas_ap['vpc'])){
                    $json['error'] .= $this->language->get('error_vpc')."<br/>";
                }
                if($instance_count > ($quotas_limits_ap['instance'] - $quotas_ap['instance'])){
                    $json['error'] .= $this->language->get('error_instance')."<br/>";
                }
		        
            	
            	
            	
            }
            


        }





        if(!$json){
            $json['result']='success';
        }

        $this->response->setOutput(json_encode($json));

    }

}
?>